<template>
  <div>
    <el-dialog :title="info.isAdd ?'规格添加':'规格修改'" :visible.sync="info.isshow" @close="close">
      user:{{user}}
      attrsArr:{{attrsArr}}
      <el-form :model="user">
        <el-form-item label="规格名称" label-width="120px">
          <el-input v-model="user.specsname" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="规格属性" label-width="120px" v-for="(item,index) in attrsArr" :key="index">
        <div class="inputAndBtn">
          <el-input v-model="item.val" autocomplete="off"></el-input>
          <el-button type="primary" v-if="index==0" @click="attrsArr.push({val:''})">新增规格属性</el-button>
          <el-button type="danger" v-else @click="attrsArr.splice(index,1)">删除</el-button>
        </div>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add()">添加</el-button>
        <el-button type="primary" v-else @click="update">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { IndexRoutes } from "../../../router/index";
import { successAlert, errorAlert } from "../../../utils/alert.js";
import { reqspecsinfo, reqspecsedit,reqspecsadd } from "../../../http/api";
import path from "path";
import {
  reqcateadd
} from "../../../http/api";
import { formClose } from "../../../mixins";

export default {
  //接受info
  props: ["info"],
  mixins: [formClose],
  data() {
    return {
      //初始化表单数据
      user: {
        specsname: "",
        attrs: "",
        status: 1
      },
      attrsArr:[
        {
          val:""
        },
        {
          val:""
        }
      ]
    };
  },
  computed: {
    ...mapGetters({
    })
  },
  mounted() {},
  methods: {
    ...mapActions({
      changeUserInfo: "changeUserInfo",
      reqArr: "specs/reqArr"
    }),

    //清空user
    empty() {
      this.user = {
        specsname: "",
        attrs: "",
        status: 1
      };
      this.attrsArr=[
        {
          val:""
        },
          {
          val:""
        }
      ]
    },
    checkData(){
      //先后顺序用Promise
      return new Promise((resolve,reject)=>{
       if(this.user.specsname==""){
         errorAlert("规格名称不能为空")
         return
       }
       if(this.attrsArr.some(item=>item.val=="")){
         errorAlert("规格属性不能为空")
         return;
       }
       //set去重
       if(new Set(this.attrsArr.map(item=>item.val)).size < this.attrsArr.length){
        errorAlert("规格属性不能重复")
         return
       }
       resolve()
      })
    },
    //点了添加
    add() {
       
     this.checkData().then(()=>{
      //处理attrs
      this.user.attrs = this.attrsArr.map(item=>(item.val)).join(",")

      reqspecsadd(this.user).then(res => {
        if (res.data.code == 200) {
          //弹成功
          successAlert(res.data.msg);
          //弹框清空
          this.empty();
          //弹框消失
          this.info.isshow = false;
          //重新请求列表
          this.reqArr()
        }
      });
     })
 


    },
    //上级菜单发生了改变
    changepid() {
      //如果上级菜单是顶级菜单，那么类型就是目录。否则就是菜单，看pid
      if (this.user.pid == 0) {
        this.user.type = 1;
      } else {
        this.user.type = 2;
      }
    },
    //获取一条数据
    getOne(id) {
      reqspecsinfo({ id: id }).then(res => {
        if (res.data.code == 200) {
          this.user = res.data.list[0];
          this.attrsArr = this.user.attrs.map(item=>({val:item}))
        }
      });
    },
    // 修改
    update() {
      this.checkData().then(()=>{
      this.user.attrs = this.attrsArr.map(item=>(item.val)).join(",")
      reqspecsedit(this.user).then(res => {
        if (res.data.code == 200) {
          //弹成功
          successAlert(res.data.msg);
          //弹框清空
          this.empty();
          //弹框消失
          this.info.isshow = false;
          //重新请求列表
          this.reqArr();
        }
      });
      })

    }
  }
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
.inputAndBtn{
   display: flex;
   .el-button{
     width: auto;
   }
   .el-input{
     flex: 1;
   }
}
</style>